package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.system.service.IGuestbookReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GuestbookAskMapper;
import com.ruoyi.system.domain.GuestbookAsk;
import com.ruoyi.system.service.IGuestbookAskService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-07
 */
@Service
public class GuestbookAskServiceImpl implements IGuestbookAskService 
{
    @Autowired
    private GuestbookAskMapper guestbookAskMapper;

    @Autowired
    private IGuestbookReplyService ddGuestbookReplyService;

    /**
     * 查询【请填写功能名称】
     * 
     * @param askid 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public GuestbookAsk selectGuestbookAskByAskid(String askid)
    {
        return guestbookAskMapper.selectGuestbookAskByAskid(askid);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param guestbookAsk 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<GuestbookAsk> selectGuestbookAskList(GuestbookAsk guestbookAsk)
    {
        return guestbookAskMapper.selectGuestbookAskList(guestbookAsk);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param guestbookAsk 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertGuestbookAsk(GuestbookAsk guestbookAsk)
    {
        return guestbookAskMapper.insertGuestbookAsk(guestbookAsk);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param guestbookAsk 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateGuestbookAsk(GuestbookAsk guestbookAsk)
    {
        return guestbookAskMapper.updateGuestbookAsk(guestbookAsk);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param askids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteGuestbookAskByAskids(String[] askids)
    {
        return guestbookAskMapper.deleteGuestbookAskByAskids(askids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param askid 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteGuestbookAskByAskid(String askid)
    {
        return guestbookAskMapper.deleteGuestbookAskByAskid(askid);
    }

    //查询公开的信箱留言
    @Override
    public List<Map<String, Object>> findPublic(int ppStartIndex,int ppPageSize,int ppIsHuifu,String ppNeirong) {
        ppNeirong = "%"+ppNeirong+"%";
        List<Map<String, Object>> mmGuestbookAskList = guestbookAskMapper.findPublic(ppStartIndex, ppPageSize,ppIsHuifu,ppNeirong);
        List<Map<String, Object>> mmReturnList = new ArrayList<Map<String,Object>>();

        for (int i = 0; i < mmGuestbookAskList.size(); i++) {
            Map<String, Object> mmGuestbookAsk = mmGuestbookAskList.get(i);
            String mmAskId = mmGuestbookAsk.get("askid") == null ? "":mmGuestbookAsk.get("askid").toString();

            String mmShifouhuifu = "已回复";
            if (ppIsHuifu==2) {
                if (ddGuestbookReplyService.findByAskId(mmAskId).size()==0) {
                    mmShifouhuifu = "未回复";
                    mmGuestbookAsk.put("shifouhuifu", mmShifouhuifu);

                    Map<String,Object> mmMap = new HashMap<>();
                    mmMap.put("ask", mmGuestbookAsk);
                    mmMap.put("replylist",ddGuestbookReplyService.findByAskId(mmAskId));
                    mmReturnList.add(mmMap);
                }
                if (ddGuestbookReplyService.findByAskId(mmAskId).size()!=0) {
                    mmGuestbookAsk.put("shifouhuifu", mmShifouhuifu);

                    Map<String,Object> mmMap = new HashMap<>();
                    mmMap.put("ask", mmGuestbookAsk);
                    mmMap.put("replylist",ddGuestbookReplyService.findByAskId(mmAskId));
                    mmReturnList.add(mmMap);
                }
            }
            else if (ppIsHuifu==0) {
                if (ddGuestbookReplyService.findByAskId(mmAskId).size()==0) {
                    mmShifouhuifu = "未回复";
                    mmGuestbookAsk.put("shifouhuifu", mmShifouhuifu);

                    Map<String,Object> mmMap = new HashMap<>();
                    mmMap.put("ask", mmGuestbookAsk);
                    mmMap.put("replylist",ddGuestbookReplyService.findByAskId(mmAskId));
                    mmReturnList.add(mmMap);
                }
            }else if(ppIsHuifu==1) {
                if (ddGuestbookReplyService.findByAskId(mmAskId).size()!=0) {
                    mmGuestbookAsk.put("shifouhuifu", mmShifouhuifu);

                    Map<String,Object> mmMap = new HashMap<>();
                    mmMap.put("ask", mmGuestbookAsk);
                    mmMap.put("replylist",ddGuestbookReplyService.findByAskId(mmAskId));
                    mmReturnList.add(mmMap);
                }
            }
        }

        return mmReturnList;
    }

    @Override
    public int countPublic(int ppStartIndex,int ppPageSize,int ppIsHuifu,String ppNeirong) {
        ppNeirong = "%"+ppNeirong+"%";
        List<Map<String, Object>> mmGuestbookAskList = guestbookAskMapper.findPublic(ppStartIndex, ppPageSize,ppIsHuifu,ppNeirong);
        List<Map<String, Object>> mmReturnList = new ArrayList<Map<String,Object>>();

        for (int i = 0; i < mmGuestbookAskList.size(); i++) {
            Map<String, Object> mmGuestbookAsk = mmGuestbookAskList.get(i);
            String mmAskId = mmGuestbookAsk.get("askid") == null ? "":mmGuestbookAsk.get("askid").toString();

            String mmShifouhuifu = "已回复";
            if (ppIsHuifu==2) {
                if (ddGuestbookReplyService.findByAskId(mmAskId).size()==0) {
                    mmShifouhuifu = "未回复";
                    mmGuestbookAsk.put("shifouhuifu", mmShifouhuifu);
                    mmReturnList.add(mmGuestbookAsk);
                }
                if (ddGuestbookReplyService.findByAskId(mmAskId).size()!=0) {
                    mmGuestbookAsk.put("shifouhuifu", mmShifouhuifu);
                    mmReturnList.add(mmGuestbookAsk);
                }
            }
            else if (ppIsHuifu==0) {
                if (ddGuestbookReplyService.findByAskId(mmAskId).size()==0) {
                    mmShifouhuifu = "未回复";
                    mmGuestbookAsk.put("shifouhuifu", mmShifouhuifu);
                    mmReturnList.add(mmGuestbookAsk);
                }
            }else if(ppIsHuifu==1) {
                if (ddGuestbookReplyService.findByAskId(mmAskId).size()!=0) {
                    mmGuestbookAsk.put("shifouhuifu", mmShifouhuifu);
                    mmReturnList.add(mmGuestbookAsk);
                }
            }
        }

        return mmReturnList.size();
    }


    //查询本管理员信箱留言列表
    @Override
    public int count() {
        Object mmCount = guestbookAskMapper.getCount();
        return (mmCount==null)?0:Integer.parseInt(mmCount.toString());
    }

    @Override
    public List<Map<String, Object>> findByAll(int ppStartIndex, int ppPageSize) {
        List<Map<String, Object>> mmGuestbookAskList = guestbookAskMapper.findByAll(ppStartIndex, ppPageSize);
        List<Map<String, Object>> mmReturnList = new ArrayList<Map<String,Object>>();

        for (int i = 0; i < mmGuestbookAskList.size(); i++) {
            Map<String, Object> mmGuestbookAsk = mmGuestbookAskList.get(i);
            String mmAskId = mmGuestbookAsk.get("askid") == null ? "":mmGuestbookAsk.get("askid").toString();

            String mmShifouhuifu = "已回复";
            if (ddGuestbookReplyService.findByAskId(mmAskId).size()==0) {
                mmShifouhuifu = "未回复";
            }

            mmGuestbookAsk.put("shifouhuifu", mmShifouhuifu);

            Map<String,Object> mmMap = new HashMap<>();
            mmMap.put("ask", mmGuestbookAsk);
            mmMap.put("replylist",ddGuestbookReplyService.findByAskId(mmAskId));

            mmReturnList.add(mmMap);
        }

        return mmReturnList;
    }
}
